package io.gitee.zicai.service.consumer.config

import feign.Logger
import feign.codec.Encoder
import feign.form.spring.SpringFormEncoder
import org.springframework.beans.factory.ObjectFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.http.HttpMessageConverters
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.cloud.openfeign.support.SpringEncoder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestTemplate
import java.time.Duration

/**
 * FeignConfig
 * @author zicai
 * @since 2019-12-05
 */
@Configuration
@EnableFeignClients("io.gitee.zicai.service.consumer.feign")
open class FeignConfig {

    /**
     * 负载均衡
     */
    @Bean
    @LoadBalanced
    open fun restTemplate() : RestTemplate = RestTemplateBuilder()
            .setConnectTimeout(Duration.ofMinutes(3))
            .build()

    /**
     * 开启feign日志
     */
    @Bean
    @ConditionalOnMissingBean
    open fun feignLoggerLevel() = Logger.Level.FULL

    /**
     * 支持feign的文件上传
     */
    @Bean
    open fun multipartFormEncoder(messageConverters: ObjectFactory<HttpMessageConverters?>?): Encoder? {
        return SpringFormEncoder(SpringEncoder(messageConverters))
    }

}